home *** CD-ROM | disk | FTP | other *** search
- Path: news.rain.org!usenet
- From: "Guus Leeuw jr." <guusl@eiffel.com>
- Newsgroups: comp.lang.c
- Subject: Re: More Modulus questions
- Date: Mon, 26 Feb 1996 08:53:52 -0800
- Organization: Interactive Software Engineering Inc. http://www.eiffel.com/
- Message-ID: <3131E5A0.6134B976@eiffel.com>
- References: <Pine.SOL.3.90.960219171637.21117B-100000@eddie> <4gfnka$ni7@spanky.pls.ov.com> <4gfp5a$r8e@cloner2.ix.netcom.com>
- NNTP-Posting-Host: @outback.eiffel.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (X11; I; Linux 1.2.8 i586)
-
- KPN wrote:
- >
- [snip snip]
- >
- > #include <stdio.h>
- >
- > main()
- > {
- > int number;
- > int first, second, third, fourth;
- > int integer1, integer2, integer3, integer4;
- >
- > printf("Enter a four digit number: ");
- > scanf("%d", &number);
- >
- > /*
- > Split the four digit number into four seperate integers
- > */
- >
- > first = number / 1000;
- > integer1 = number % 1000;
- >
- > second = integer1 / 100;
- > integer2 = integer1 % 100;
- >
- > third = integer2 / 10;
- > integer3 = integer2 % 10;
- >
- > fourth = integer3 / 1;
- > integer4 = integer3 % 1;
- >
-
- Add these two lines:
- printf("%d %d %d %d\n", first, second, third, fourth);
- printf("%d %d %d %d\n", first%7, second%7, third%7, fourth%7);
-
- > /*
- > Use modulas to determine if any integers are a 7
- > */
- >
- > if (first % 7)
- > printf("First integer was a 7\n");
- > else
- > printf("Not a 7\n");
- >
- > if (second % 7)
- > printf("second integer was a 7\n");
- > else
- > printf("Not a 7\n");
- >
- > if (third % 7)
- > printf("third integer was a 7\n");
- > else
- > printf("Not a 7\n");
- >
- > if (fourth % 7)
- > printf("fourth integer was a 7\n");
- > else
- > printf("Not a 7\n");
- >
- [snip snip]
-
- From the added printf statements, you can see that something is wrotten.
- 7%7 gives 0. Modulo is the remainder after division.
- You're test whether 7%7 is not 0, which is wrong.
-
- 0%77 will also give 0, and that's something you really don't want.
-
- I think, there's just one way to determine whether 7 is 7 and that is either `7 == 7'
- or '7 % 10 == 7'.
-
- In the above text each first 7 in each expression is to be replaced with `first' ..
- `fourth'.
-
- Hope this helps,
- Guus
-